math.randomseed(os.time())

require("utils.remote_debug").start(8172)
local skynet = require "skynet"
require "skynet.manager"
local msg_center = require "agents.msg_center"
local MessageType = require "common.message_type"
local watchdog_proxy = require "agents.watchdog_proxy"
local service_helper = require "common.service_helper"

-- require msg handler begin
require "agents.login_msg_handler"
require "agents.game_msg_handler"

-- require msg handler end

local CMD = {}

CMD[MessageType.Inited] = function(watchdog)
    watchdog_proxy.set_watchdog(watchdog)
end

CMD[MessageType.OnClientConnected] = function(...)
    skynet.error("WatchdogMessageId OnConnected")
    msg_center.dispatchWatchdogMessage(MessageType.OnClientConnected,...)
end

CMD[MessageType.OnClientDisconnected] = function(...)
    skynet.error("WatchdogMessageId OnDisconnected")
    msg_center.dispatchWatchdogMessage(MessageType.OnClientDisconnected,...)
end

skynet.start(function()
    skynet.dispatch("lua", function(_,_, command, ...)
        skynet.trace()
        local f = CMD[command]
        if f then
            skynet.ret(skynet.pack(f(...)))
        else
            msg_center.dispatchDefaultMessage(command,...)
            skynet.ret()
        end
    end)
    skynet.register(service_helper.services.agents)
end)
